﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using AzureDiagMon.Common.Mvvm;
using AzureDiagMon.Common.Mvvm.Interfaces;
using Ninject.Modules;

namespace AzureDiagMon.ViewModels
{
    public class Module : NinjectModule
    {
        #region Overrides of NinjectModule

        public override void Load()
        {
            Bind<MainViewModel>().ToSelf().InSingletonScope();
            var a = Assembly.GetExecutingAssembly();
            var viewModelTypes = a.GetTypes().Where(f => f.IsSubclassOf(typeof(ViewModel)) && f != typeof(MainViewModel));
            foreach (var viewModelType in viewModelTypes)
            {
                var attr =
                    viewModelType.GetCustomAttributes(typeof(ViewModelAttribute), false)
                    .FirstOrDefault() as ViewModelAttribute;
                if (attr != null && !String.IsNullOrEmpty(attr.Name))
                    Bind<IViewModel>().To(viewModelType).Named(attr.Name);
                else
                    Bind<IViewModel>().To(viewModelType).Named(viewModelType.Name);
            }
        }

        #endregion
    }
}
